package com.ly.gof.structure.proxy.dynamic;

import java.lang.reflect.Proxy;

/**
 * @author liuyang
 * @des 利用jdk 实现动态代理
 * @Version 1.0
 * @Date: 21/07/2021 15:23
 */
public class Test {
    public static void main(String[] args) {
        RealSubject realSubject = new RealSubject();
        //handle自己的方法获取代理对象
        ProxyHandler proxyHandler = new ProxyHandler();
        Subject subject = proxyHandler.getInstance(realSubject);
        //利用jdk提供的Proxy.newProxyInstance方法获取代理对象
        ProxyHandler proxyHandler1 = new ProxyHandler(realSubject);
        Subject subject1 = (Subject)Proxy
            .newProxyInstance(realSubject.getClass().getClassLoader(), realSubject.getClass().getInterfaces(),
                proxyHandler1);
        subject.request();
        subject1.request();
        // 保存JDK动态代理生成的代理类，类名保存为 UserServiceProxy
        ProxyUtils.generateClassFile(realSubject.getClass(), "SubjectProxy");
    }
}
